#include "ResourceManager.h"

CResourceManager* CResourceManager::mInstance = 0;
CResourceManager::CResourceManager()
{

}
CResourceManager::~CResourceManager()
{
	std::vector<CCSpriteBatchNode*>::iterator it;
	for(it = mBatchNode.begin();it!=mBatchNode.end();)
	{
		(*it)->release();
		it = mBatchNode.erase(it);
	}
}
void CResourceManager::LoadResource()
{
	mBatchNode.clear();
	CCSpriteBatchNode* batchNode;
	batchNode = CCSpriteBatchNode::create("./arrow.png");
	mBatchNode.push_back(batchNode);
	batchNode->retain();

	batchNode = CCSpriteBatchNode::create("./circle.png");
	mBatchNode.push_back(batchNode);
	batchNode->retain();
	 
	batchNode = CCSpriteBatchNode::create("./range.png");
	mBatchNode.push_back(batchNode);
	batchNode->retain();

	batchNode = CCSpriteBatchNode::create("./aim.png");
	mBatchNode.push_back(batchNode);
	batchNode->retain();

	batchNode = CCSpriteBatchNode::create("./star.png");
	mBatchNode.push_back(batchNode);
	batchNode->retain();

	
	CCDictionary* utilDic = dynamic_cast<CCDictionary*>(gPlistDic->objectForKey("util"));
	mBucketBodyList.clear();
	CCArray *_array = (CCArray*)(utilDic->objectForKey("bucketBody") );
	int count = _array->count();
	for (int i=0; i<count; i++) {
		CCString *tmpStr = (CCString*)_array->objectAtIndex(i);
		CCRect rect =  CCRectFromString(tmpStr->getCString());
		mBucketBodyList.push_back(rect);
	}

	_array = (CCArray*)(utilDic->objectForKey("bucketCollision") );
	count = _array->count();
	mBucketCollision.clear();
	for (int i=0; i<count; i++) {
		CCString *tmpStr = (CCString*)_array->objectAtIndex(i);
		CCRect rect =  CCRectFromString(tmpStr->getCString());
		mBucketCollision.push_back(rect);
	}
}

CCTexture2D* CResourceManager::GetTexture(BatchNodeTex texType)
{
	if(texType >= TotalTex)
		return NULL;
	return mBatchNode[texType]->getTexture();
}

b2Body* CResourceManager::GetBody(BodyType bodyType)
{
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	b2EdgeShape groundBoxEdge;
	b2Body* _body;
	b2BodyDef groundBodyDef;
	groundBodyDef.position.Set(0, 0);
	_body = gBoxWorld->CreateBody(&groundBodyDef);
	if(bodyType == BucketBody)
	{
		int count = mBucketBodyList.size();
		for (int i=0; i<count; i++) {
			CCRect rect = mBucketBodyList[i];
			groundBoxEdge.Set(b2Vec2( (rect.origin.x * winSize.width)/kPTM_RATIO,  rect.origin.y*winSize.height/kPTM_RATIO), 
				b2Vec2( (rect.size.width * winSize.width)/kPTM_RATIO, rect.size.height*winSize.height/kPTM_RATIO) );
			_body->CreateFixture(&groundBoxEdge, 0);
		}
		return _body;
	}
	else if(BucketCollision == bodyType)
	{
		int count = mBucketCollision.size();
		for (int i=0; i<count; i++) {
			CCRect rect = mBucketCollision[i];
			groundBoxEdge.Set(b2Vec2( (rect.origin.x * winSize.width)/kPTM_RATIO,  rect.origin.y*winSize.height/kPTM_RATIO), 
				b2Vec2( (rect.size.width * winSize.width)/kPTM_RATIO, rect.size.height*winSize.height/kPTM_RATIO) );
			_body->CreateFixture(&groundBoxEdge, 0);
		}
		return _body;
	}
	return NULL;
}